package com.haiqi.modules.main.service;

import com.alibaba.fastjson.JSON;
import com.haiqi.common.utils.DesUtils;
import com.haiqi.modules.main.bean.MenuResponse;
import com.haiqi.modules.main.bean.ResponseBean;
import com.haiqi.webservice.AuthStub;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by hythzx on 2016/5/20.
 */

@Service
public class UserService {

    @Value("${appCode}")
    private String appCode;

    private Logger logger = Logger.getLogger(this.getClass());

    public UserService() {
    }

    public UserService(String code){
        this.appCode = code;
    }

    /**
     * 用户登录
     * @param userName
     * @param password
     * @return
     * @throws Exception
     */
    public ResponseBean login(String userName,String password) throws Exception {
        ResponseBean responseBean = new ResponseBean();
        AuthStub.Verify verify = new AuthStub.Verify();
        verify.setUsername(DesUtils.encrypt(userName));
        verify.setPassword(DesUtils.encrypt(password));
        AuthStub.VerifyE verifyE = new AuthStub.VerifyE();
        verifyE.setVerify(verify);
        com.haiqi.webservice.AuthStub stub = null; //the default implementation should point to the right endpoint
        stub = new com.haiqi.webservice.AuthStub();
        // TODO : Fill in the verify14 here
        AuthStub.VerifyResponseE responseE = stub.verify(verifyE);
        String aReturn = responseE.getVerifyResponse().get_return();
        String encrypt = DesUtils.decrypt(aReturn,DesUtils.key);
        System.out.println(encrypt);
        responseBean =  JSON.parseObject(encrypt,ResponseBean.class);
        return responseBean;
    }

    /**
     * 获取菜单列表
     * @param userId
     * @param token
     * @return
     * @throws Exception
     */
    public MenuResponse getMenus(String userId,String token) throws Exception {
        AuthStub.Menu menu = new AuthStub.Menu();
        menu.setAppCode(DesUtils.encrypt(appCode));
        menu.setToken(DesUtils.encrypt(token));
        menu.setUserId(DesUtils.encrypt(userId));
        AuthStub.MenuE menuE = new AuthStub.MenuE();
        menuE.setMenu(menu);
        AuthStub authStub = new AuthStub();
        AuthStub.MenuResponseE menuResponseE = authStub.menu(menuE);
        String aReturn = menuResponseE.getMenuResponse().get_return();
        logger.info("服务器端返回的数据是：");
        logger.info(aReturn);
        System.out.println(DesUtils.decrypt(aReturn,DesUtils.key));
        MenuResponse menuResponse = JSON.parseObject(DesUtils.decrypt(aReturn,DesUtils.key), MenuResponse.class);
        return menuResponse;
    }
}
